<?php
//this class handles cron jobs that are specific to the delivery cycle

class Job extends AppModel {

	var $name = 'Job';
//	var $hasOne = array();
//	var $hasMany = array();
//	var $belongsTo = array();
//	var $hasAndBelongsToMany = array();

//	function beforeSave($data) {}

//	function afterSave() {}

	function getTodaysJobs() {
		$jobs = $this->findAll();
		$loadedModels = array();
		$timeFormat = 'Y-m-d H:i:s';
		$ret = array();
		foreach($jobs as $job) {
			$model = null;
			if (!in_array($job['Job']['model'], $loadedModels)) {
				App::import('Model', $job['Job']['model']);
				$loadedModels[] = $job['Job']['model'];
			}
			$model = new $job['Job']['model']();
			$findStartDate = strtotime('today') - (86400 * $job['Job']['offset']);
			$findEndDate = $findStartDate + 86400;
			$conditions = array(array($job['Job']['model'] . '.' . $job['Job']['field'] => '>=' . date($timeFormat, $findStartDate)),
				array($job['Job']['model'] . '.' . $job['Job']['field'] => '< ' . date($timeFormat, $findEndDate)));
			$runFor = $model->find($conditions);
			foreach($runFor as $running) {
				$job['running'] = $running;
				$ret[] = $job;
			}
		}
		return $ret;
	}

}

?>